package com.xiaoshuidi.cloud.module.iot.controller.admin.ammeter;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.iot.annotation.OperationDataAccessAnnotation;
import com.xiaoshuidi.cloud.module.iot.api.device.vo.AmmeterInfoResModel;
import com.xiaoshuidi.cloud.module.iot.common.ResultModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.ammeter.*;
import com.xiaoshuidi.cloud.module.iot.dal.model.watermeter.details.WaterMeterReportRequestModel;
import com.xiaoshuidi.cloud.module.iot.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.iot.framework.redislock.annoation.Lock4j;
import com.xiaoshuidi.cloud.module.iot.service.AmmeterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 电表相关接口
 * @author Sky
 * create 2019/04/11
 * email sky.li@ixiaoshuidi.com
 **/
@Tag(name = "智能设备 - 智能电表")
@RestController
@RequestMapping(ApiConstants.NAME + "/ammeter")
public class AmmeterController {

    private final static Logger LOGGER = LoggerFactory.getLogger(AmmeterController.class);


    @Autowired
    private AmmeterService ammeterService;

    /**
     * 电表详情
     *
     * @param ammeterInfoReqModel
     * @return
     */
    @Operation(summary = "电表详情")
    @Parameter(name = "ammeterInfoReqModel", description = "电表实体类")
    @PostMapping("/manage/getAmmeterInfo")
    public ResultModel<AmmeterInfoResModel> getAmmeterInfo(@RequestBody AmmeterInfoReqModel ammeterInfoReqModel) {
        ResultModel<AmmeterInfoResModel> resultModel = null;
        try {
            resultModel = ammeterService.getAmmeterInfo(ammeterInfoReqModel);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("ammeter->获取电表详情异常,请求参数：{}，异常原因：{}", JSONObject.toJSONString(ammeterInfoReqModel), e.getMessage());
            resultModel = ResultModel.createNativeFail(e.getMessage());
        }
        return resultModel;
    }

    /**
     * 刷新电表详情
     *
     * @param ammeterInfoReqModel
     * @return
     */
    @Operation(summary = "刷新电表详情")
    @Parameter(name = "ammeterInfoReqModel", description = "电表实体类")
    @PostMapping("/manage/refreshAmmeterInfo")
    @Lock4j(keys = {"#ammeterInfoReqModel.deviceNo"}, expire = 60000, message = "请勿重复刷新")
    public ResultModel<AmmeterInfoResModel> refreshAmmeterInfo(@RequestBody AmmeterInfoReqModel ammeterInfoReqModel) {
        ResultModel<AmmeterInfoResModel> resultModel = null;
        try {
            resultModel = ammeterService.refreshAmmeterInfo(ammeterInfoReqModel);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("ammeter->刷新电表详情,请求参数：{}，异常原因：{}", JSONObject.toJSONString(ammeterInfoReqModel), e.getMessage());
            resultModel = ResultModel.createNativeFail(e.getMessage());
        }
        return resultModel;
    }

    /**
     * 设置电表合闸
     *
     * @param ammeterSwitchOnOffReqVO
     * @return
     */
    @Operation(summary = "设置电表合闸")
    @Parameter(name = "ammeterSwitchOnOffReqVO", description = "电表合闸实体类")
    @OperationDataAccessAnnotation
    @PostMapping("/manage/setAmmeterSwitchOn")
    public ResultModel<AmmeterSwitchOnOffResVO> setAmmeterSwitchOn(@RequestBody AmmeterSwitchOnOffReqVO ammeterSwitchOnOffReqVO) {
        ResultModel<AmmeterSwitchOnOffResVO> resultModel = null;
        try {
            resultModel = ammeterService.setAmmeterSwitchOn(ammeterSwitchOnOffReqVO);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("ammeter->设置电表合闸,请求参数：{}，异常原因：{}", JSONObject.toJSONString(ammeterSwitchOnOffReqVO), e.getMessage());
            resultModel = ResultModel.createNativeFail(e.getMessage());
        }
        return resultModel;
    }


    /**
     * 设置电表跳闸
     *
     * @param ammeterSwitchOnOffReqVO
     * @return
     */
    @Operation(summary = "设置电表跳闸")
    @Parameter(name = "ammeterSwitchOnOffReqVO", description = "电表跳闸实体类")
    @OperationDataAccessAnnotation
    @PostMapping("/manage/setAmmeterSwitchOff")
    public ResultModel<AmmeterSwitchOnOffResVO> setAmmeterSwitchOff(@RequestBody AmmeterSwitchOnOffReqVO ammeterSwitchOnOffReqVO) {
        ResultModel<AmmeterSwitchOnOffResVO> resultModel = null;
        try {
            resultModel = ammeterService.setAmmeterSwitchOff(ammeterSwitchOnOffReqVO);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("ammeter->设置电表跳闸,请求参数：{}，异常原因：{}", JSONObject.toJSONString(ammeterSwitchOnOffReqVO), e.getMessage());
            resultModel = ResultModel.createNativeFail(e.getMessage());
        }
        return resultModel;
    }

    /**
     * 电表用电明细
     *
     * @param ammeterReportReqVO
     * @return
     */
    @Operation(summary = "电表用电明细")
    @Parameter(name = "ammeterReportReqVO", description = "电表明细实体类")
    @PostMapping("/manage/getAmmeterReport")
    public ResultModel<AmmeterReportResVO> getAmmeterReport(@RequestBody WaterMeterReportRequestModel ammeterReportReqVO) {
        ResultModel<AmmeterReportResVO> resultModel = null;
        try {
            resultModel = ammeterService.getAmmeterReport(ammeterReportReqVO);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("ammeter->电表用电明细,请求参数：{}，异常原因：{}", JSONObject.toJSONString(ammeterReportReqVO), e.getMessage());
            resultModel = ResultModel.createNativeFail(e.getMessage());
        }
        return resultModel;
    }

}

